package ru.braim.weather_app.controllers;

import jakarta.annotation.Nullable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ru.braim.weather_app.dto.region_type.RegionTypeRequestDTO;
import ru.braim.weather_app.dto.region_type.RegionTypeResponseDTO;
import ru.braim.weather_app.responses.region_type.RegionTypeResponse;
import ru.braim.weather_app.services.CookieService;
import ru.braim.weather_app.services.RegionTypeService;

@RestController
@RequestMapping("/region/types")
public class RegionTypeController {

    private final RegionTypeService regionTypeService;
    private final CookieService cookieService;

    public RegionTypeController(RegionTypeService regionTypeService, CookieService cookieService) {
        this.regionTypeService = regionTypeService;
        this.cookieService = cookieService;
    }

    @PostMapping
    public ResponseEntity<RegionTypeResponseDTO> createRegionType(@RequestBody RegionTypeRequestDTO regionTypeRequestDTO,
                                                                  @CookieValue("id") @Nullable String cookie) {
        if (!cookieService.checkAuthorizeCookie(cookie)) {
            return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
        }

        RegionTypeResponse regionTypeResponse = regionTypeService.createRegionType(regionTypeRequestDTO);

        return new ResponseEntity<>(regionTypeResponse.getRegionTypeResponseDTO(), regionTypeResponse.getHttpStatus());

    }


    @GetMapping("/{typeID}")
    public ResponseEntity<RegionTypeResponseDTO> getRegionTypeById(@PathVariable String typeID,
                                                                   @CookieValue("id") @Nullable String cookie) {

        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            RegionTypeResponse regionTypeResponse = regionTypeService.getRegionTypeById(Long.parseLong(typeID));
            return new ResponseEntity<>(regionTypeResponse.getRegionTypeResponseDTO(),
                    regionTypeResponse.getHttpStatus());
        }
        catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

    @PutMapping("/{typeID}")
    public ResponseEntity<RegionTypeResponseDTO> updateRegionById(@PathVariable String typeID,
                                                                  @RequestBody RegionTypeRequestDTO regionTypeRequestDTO,
                                                                  @CookieValue("id") @Nullable String cookie) {
        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            RegionTypeResponse regionTypeResponse = regionTypeService.updateRegionType(Long.parseLong(typeID),
                    regionTypeRequestDTO);
            return new ResponseEntity<>(regionTypeResponse.getRegionTypeResponseDTO(),
                    regionTypeResponse.getHttpStatus());
        }
        catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
    }

    @DeleteMapping("/{typeID}")
    public ResponseEntity<RegionTypeResponseDTO> deleteRegionById(@PathVariable String typeID,
                                                                  @CookieValue("id") @Nullable String cookie) {
        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            RegionTypeResponse regionTypeResponse = regionTypeService.deleteRegionTypeById(Long.parseLong(typeID));
            return new ResponseEntity<>(regionTypeResponse.getRegionTypeResponseDTO(),
                    regionTypeResponse.getHttpStatus());
        }
        catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
    }

}
